var express = require('express');
var router = express.Router();

const db = require("../utils/dbconfig")

//更新数据
router.post('/', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");

  console.log("req.body:", req.body)
  let list = req.body
  let sentence = ''
  let params = []
  for (let key in list) {
    if (list[key]) {
      sentence += `${key}=?,`
      params.push(list[key])
    }
  }
  sentence = sentence.slice(0, -1);//去掉末尾的逗号
  console.log("sentence:" + sentence)
  let sql = `update user set ${sentence} where userId =${req.body.userId} `;
  console.log("sql:" + sql)
  console.log("params:", params)

  db.sqlConnection(sql, params).then((res) => {
    console.log('res:', res)
  }).catch((err) => {
    console.log(err)
  })
  data = '更新成功'
  res.send(data)
});

module.exports = router;